'use strict';

const { exec, spawn } = require('child_process');

/**
 * 睡眠等待ms相应
 * @params { number } ms
 * **/
exports.sleep = function (ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
};

/**
 * @desc 根据进程id 杀死进程
 * @param  { number } pid 进程id
 */
exports.killProcess = function (pid) {
  if (!pid) return new Error('no pid');
  spawn('taskkill', ['/pid', pid, '/f', '/t']);
};

/**
 * @desc 通过默认浏览器打开链接
 * @param  { string } url
 */
exports.openDefaultBrowser = function (url) {
  switch (process.platform) {
    case 'drawin':
      return exec('open ' + url);
    case 'win32':
      return exec('start ' + url);
    default:
      return exec('xdg-open ', [url]);
  }
};
